<script setup lang="ts">
/* @jsxImportSource vue */
import {storeToRefs} from 'pinia'
import {computed, h, inject, ref} from 'vue'
import katex from 'katex'
import {NButton, NConfigProvider, NEquation, NInput, NSelect} from 'naive-ui'
import {useSgesMAStore} from './MountainGesCalA.ts'

const props = defineProps<{ // withDefaults给defineProps中的参数绑定默认值
  create3D?: Function // 创建3D模型的回调函数
}>()

const store = useSgesMAStore()
const {
  timeAccerlate,
  forceDrive,
  frictionForce,
  acceleration, // 添加到返回对象
  slopeDistance,
  totalRunningTime,
  calInfo,
  numBlocksOnSingleRail,
  integerNumBlocksOnSingleRail,
  areaControlRoom,
  etaCharge_100,
  etaDischarge_100,
  etaSystem_100,
  capChargeTotalSta,
  capDischargeTotalSta,
  powerChargeTotalSta,
  totalChargeTime,
  cal_power,
  vibrant_para,
  vibrant_option,
  heightMT,
  slopeDegree,
  density,
  lengthBlock,
  widthBlock,
  heightBlock,
  etaSystem,
  volumeBlock,
  massBlock,
  etaCharge,
  etaDischarge,
  totalBlocksNum,
  totalMassBlocks,
  totalGravityEnergy,
  g,
  slopeDegreeRad,
  totalGravityEnergy_MJ,
  weightBlock,
  forceWeightLocalX,
  lengthSlope,
  gapBlocks,
  velBlock,
  powerMechanic,
  timeSingleBlock,
  timeIntervalOfBlocks,
  frictionFactor,
  totalRunningTimeAct,
  powerDriveDesign,
  powerDriveMargin,
  powerDrive,
  widthSlope,
  lengthAccelerate,
  lengthStorage,
  lengthMerge,
  gap_rails,
  height_sleeper,
  width_sleeper,
  radius_wheel, thick_wheel,
} = storeToRefs(store) // 只有这样导入的变量才能在当前组件中绑定输入框修改，store.变量名的变量只能显示，无法修改


const show_pillar_settings = ref(false)
const show_area_settings = ref(false)

type rowType = {
  key: number,
  item: string,
  symbol: string,
  unit: string,
  formula: string,
  val: number | string,
  bak: string
}

const editableVal = [  // 可以编辑的数值行
  '斜坡宽度', '预留加速距离', '并链段长度', '铁轨间距', '枕木高度', '枕木宽度', "并链段长度",
  '重物小车车轮半径', '重物小车车轮厚度'

]
const editableSel = [  // 可以编辑的下拉列表行
  '钢丝绳级', '钢丝绳芯类型', '钢丝绳类型', '钢丝绳股型号'
]
const sepRow = [
  '重物运行计算', '轴的重量计算', '卷筒计算', '价格计算', '储能塔布局', '钢丝绳计算', '变速箱计算', '加减速计算',
  '需采购的设备', '重物储存区'
]
const columns = () => {
  return [

    {
      title: '项目', key: 'item',
      render(row: rowType) {
        if (row.item === '重物有效提升高度') {
          return h('span', {style: 'color: red'}, row.item)
        } else {
          return h('span', {}, row.item)
        }
      }
    },
    {
      title: '符号', key: 'symbol',
      render(row: rowType) {
        // @ts-ignore
        return h(NConfigProvider, {katex: katex},
            () => h(NEquation, {value: row.symbol, katexOptions: {output: 'mathml'}}))
      }
    },
    {
      title: '单位', key: 'unit'
    },
    {
      title: '公式', key: 'formula',
      render(row: rowType) {
        if (row.item === '承重柱直径') {
          return h(NButton, {
            onclick: () => {
              show_pillar_settings.value = true
            }
          }, () => {
            return '承重柱设置'
          })
        } else if (row.item === '储能塔总占地面积') {
          return h(NButton, {
            onclick: () => {
              show_area_settings.value = true
            }
          }, () => {
            return '设置'
          })
        } else {
          // @ts-ignore
          return h(NConfigProvider, {katex: katex},
              () => h(NEquation, {value: row.formula, katexOptions: {output: 'mathml'}}))
        }
      }
    },
    {
      title: '数值', key: 'val',
      render(row: rowType, index: number) {
        if (row.item === '重物有效提升高度') {
          return h('span', {style: 'color: red'}, row.val)
        } else if (editableVal.includes(row.item)) {
          return h(NInput, {
            value: String(row.val),
            style: 'background-color: lightgreen; padding-top: 0px; margin: 0px',
            onUpdateValue(v: string) {
              // @ts-ignore
              row.val = v // 此处不能加Number(v)，否则无法输入小数
            }
          })
        } else if (editableSel.includes(row.item)) {

        } else {
          return h('span', {}, row.val)
        }
      }
    },
    {
      title: '备注', key: 'bak',
      render(row: rowType) {
        if (row.item === '承重柱直径') {
          return h('div', {}, [
            h('div', {}, row.bak),
            h('a', {
              href: 'https://www.mohurd.gov.cn/gongkai/zhengce/zhengcefilelib/202107/20210715_761192.html',
              target: '_blank'
            }, 'GB 55001《工程结构通用规范》'),
            h('div', {}, ''),
            h('a', {
              href: 'https://www.mohurd.gov.cn/gongkai/zhengce/zhengcefilelib/202110/20211013_762454.html',
              target: '_blank'
            }, 'GB 55008 《混凝土结构通用规范》'),
            h('div', {}, ''),
            h('a', {
              href: 'https://www.mohurd.gov.cn/gongkai/zhengce/zhengcefilelib/202111/20211130_763200.html',
              target: '_blank'
            }, 'GB/T 51446 《钢管混凝土混合结构技术标准》')
          ])
        } else {
          return h('span', {}, row.bak)
        }
      }
    }

  ]
}
const tableData = ref([
  {
    key: 0,
    item: '储能容量',
    symbol: 'Cap_t',
    unit: 'MWh',
    formula: 'from\\ design',
    val: capChargeTotalSta, // 不能带.value，否则会失去响应性
    bak: '设计边界条件'
  },
  {
    key: 1,
    item: '放电容量',
    symbol: 'Cap_{td}',
    unit: 'MWh',
    formula: '=Cap_t/eta_s',
    val: capDischargeTotalSta,
    bak: ''
  },
  {
    key: 1,
    item: '储能效率',
    symbol: '\\eta_s',
    unit: '%',
    formula: 'from\\ design',
    val: etaSystem_100,
    bak: '电动发电机出口测量',
    children: [
      {
        key: 2,
        item: '充电效率',
        symbol: '\\eta_c',
        unit: '%',
        formula: '=sqrt(eta_s)',
        val: etaCharge_100,
        bak: '电动发电机出口测量'
      },
      {
        key: 3,
        item: '释能效率',
        symbol: '\\eta_d',
        unit: '%',
        formula: '=\\sqrt{\\eta_s}',
        val: etaDischarge_100,
        bak: '电动发电机出口测量'
      }
    ]
  },

  {
    key: 4,
    item: '山体高度',
    symbol: 'H_M',
    unit: 'm',
    formula: '=\\frac{1000000 M_s}{Weight_s}',
    val: heightMT
  },
  {
    key: 4,
    item: '山体坡度',
    symbol: 'Deg_M',
    unit: '°',
    formula: 'from\\ design',
    val: slopeDegree
  },
  {
    key: 4,
    item: '山体坡度(弧度)',
    symbol: 'Deg_{Mr}',
    unit: '’',
    formula: 'Deg_M/180\\times\\pi',
    val: slopeDegreeRad
  },
  {
    key: 4,
    item: '斜坡水平长度',
    symbol: 'Dx_M',
    unit: 'm',
    formula: '\\frac{H_M}{tan(Deg_{Mr})}',
    val: slopeDistance
  },
  {
    key: 4,
    item: '斜坡总长度',
    symbol: 'Dl_M',
    unit: 'm',
    formula: '\\frac{H_M}{sin(Deg_{Mr})}',
    val: lengthSlope
  },
  {
    key: 5,
    item: '斜坡宽度',
    symbol: '',
    unit: 'm',
    formula: '',
    val: widthSlope
  },
  {
    key: 4,
    item: '重物长',
    symbol: 'H_T',
    unit: 'm',
    formula: 'from\\ design',
    val: lengthBlock
  },
  {
    key: 4,
    item: '重物宽',
    symbol: 'H_T',
    unit: 'm',
    formula: 'from\\ design',
    val: widthBlock
  },
  {
    key: 4,
    item: '重物高',
    symbol: 'H_T',
    unit: 'm',
    formula: 'from\\ design',
    val: heightBlock
  },
  {
    key: 5,
    item: '单个重物体积',
    symbol: 'Vol_s',
    unit: 'm3',
    formula: '=L_s\\times W_s\\times H_s',
    val: volumeBlock
  },
  {
    key: 5,
    item: '重物密度',
    symbol: 'Density_s',
    unit: 'kg/m3',
    formula: '=L_s\\times W_s\\times H_s',
    val: density
  },
  {
    key: 6,
    item: '单个重物质量',
    symbol: 'M_s',
    unit: 'kg',
    formula: '=Vol_s\\times D_s',
    val: massBlock
  },
  {
    item: '单个重物重力',
    symbol: 'Weight_s',
    unit: 'N',
    formula: '=M_sg',
    val: weightBlock
  },

  {
    item: '重物总数量',
    symbol: 'n_w',
    unit: '个',
    formula: '=\\frac{M_t}{Weight_s}',
    val: totalBlocksNum
  },
  {
    key: 6,
    item: '重物总质量',
    symbol: 'M_t',
    unit: 'kg',
    formula: '=M_s\\times n_w',
    val: totalMassBlocks
  },
  {
    key: 7,
    item: '重物可储存总势能',
    symbol: 'GE_t', // gravity energy _ total
    unit: 'MJ',
    formula: '=Cap_t/eta_c',
    val: totalGravityEnergy_MJ
  },

  {},
  {item: '重物运行计算'},
  {
    item: '斜坡上轨道长度',
    symbol: 'H_e',
    unit: 'm',
    formula: '=\\frac{M_t}{Weight_s}',
    val: lengthSlope
  },
  {
    item: '单重物在运行方向的重力分量',
    symbol: 'F_{wlx}',
    unit: 'N',
    formula: '=Weight_s\\times sin(deg_{Mr})',
    val: forceWeightLocalX
  },
  {
    item: '重物运行速度',
    symbol: 'v_b',
    unit: 'm/s',
    formula: '',
    val: velBlock
  },
  {
    item: '运行时重物间距',
    symbol: 'g_b',
    unit: 'm',
    formula: '',
    val: gapBlocks
  },
  {
    item: '运行时轨道上重物数量',
    symbol: 'n_b',
    unit: '块',
    formula: '',
    val: numBlocksOnSingleRail
  },
  {
    item: '充电功率转换的机械功率',
    symbol: 'n_b',
    unit: 'm',
    formula: '',
    val: powerMechanic
  },
  {
    item: '运行时轨道上重物数量（取整）',
    symbol: 'n_b',
    unit: 'm',
    formula: '',
    val: integerNumBlocksOnSingleRail
  },
  {
    item: '单个重物运行时间',
    symbol: 'n_b',
    unit: 's',
    formula: '',
    val: timeSingleBlock
  },
  {
    item: '单个重物运行时间',
    symbol: 'n_b',
    unit: 'min',
    formula: '',
    val: 0 // timeSingleBlock.value / 60
  },
  {
    item: '重物进入轨道的时间间隔',
    symbol: 'n_b',
    unit: 's',
    formula: '',
    val: timeIntervalOfBlocks
  },
  {
    item: '理论重物转运总时间',
    symbol: 'n_b',
    unit: 'h',
    formula: '',
    val: totalRunningTime,
    bak: '不考虑启停过程，理论计算以匀速转运完所有重物需要的时间'
  },
  {
    item: '实际重物转运总时间',
    symbol: 'n_b',
    unit: 'h',
    formula: '',
    val: totalRunningTimeAct,
    bak: '考虑第一个重物开始进入轨道和最后一个重物完全离开轨道的系统运行总时间'
  },

  {},

  {
    item: '加速时间',
    symbol: 't_a',
    unit: 's',
    formula: 'from Design',
    val: timeAccerlate,
    bak: '启动时重物块由0加速到额定速度的时间'
  },
  {
    item: '加速度',
    symbol: 'a_1',
    unit: 'm/s2',
    formula: '\\frac{v_b}{t_a}}',
    val: acceleration
  },
  {
    item: '摩擦系数',
    symbol: '\\mu',
    unit: '',
    formula: 'from Design',
    val: frictionFactor
  },
  {
    item: '摩擦力',
    symbol: 'F_f',
    unit: 'N',
    formula: '\\mu\\times M_s\\times g',
    val: frictionForce
  },
  {
    item: '动力驱动力',
    symbol: 'F_d',
    unit: 'N',
    formula: '',
    val: forceDrive
  },
  {
    item: '理论驱动力功率',
    symbol: 'P_{drive}',
    unit: 'W',
    formula: 'from\\ system\\ feedback',
    val: powerDrive,
    bak: '系统实际输出的驱动功率'
  },
  {
    item: '驱动力功率裕度',
    symbol: 'P_{margin}',
    unit: '%',
    formula: '=\\frac{P_{drive}-P_{design}}{P_{design}} \\times 100',
    val: powerDriveMargin,
    bak: '实际功率与设计功率的差值百分比'
  },
  {
    item: '设计驱动力功率',
    symbol: 'P_{design}',
    unit: 'W',
    formula: '=F_d \\times v_b',
    val: powerDriveDesign,
    bak: '根据驱动力和速度计算的理论功率'
  },

  {
    item: '总充电时间',
    symbol: 't_c',
    unit: 'h',
    val: totalChargeTime
  },
  {
    item: '重物储存区'
  },
  {
    item: '储存区长度',
    symbol: 'L_s',
    unit: 'm',
    val: lengthStorage
  },
  {
    item: '预留加速距离',
    symbol: 'L_a',
    unit: 'm',
    val: lengthAccelerate
  },
  {
    item: '并链段长度',
    symbol: 'L_m',
    unit: 'm',
    val: lengthMerge
  },
  {
    item: '铁轨间距',
    symbol: 'Gap_{rail}',
    unit: 'm',
    val: gap_rails
  },
  {
    item: '枕木高度',
    symbol: 'H_{sleeper}',
    unit: 'm',
    val: height_sleeper
  },
  {
    item: '枕木宽度',
    symbol: 'W_{sleeper}',
    unit: 'm',
    val: width_sleeper
  },
  {
    item: '重物小车车轮半径',
    symbol: 'R_{wheel}',
    unit: 'm',
    val: radius_wheel
  },
  {
    item: '重物小车车轮厚度',
    symbol: 'W_{wheel}',
    unit: 'm',
    val: thick_wheel
  },
  {
    item: '需采购的设备'
  }


])

const window_height = window.innerHeight
const table_height = ref(10000)
const rowClassName = (row: rowType) => {
  if (sepRow.includes(row.item)) {
    return 'sep'
  } else {
    return ''
  }
}

const create3D = () => {
  /**
   * 如果父组件传入了创建3D模型的方法，则调用，否则控制台输出警告
   */
  if (props.create3D) {
    props.create3D()  //
  } else {
    console.warn('create3D not defined')
  }
}

const getColors = inject('getColors', () => {
  return {
    fgColor: '#000',
    bgColor: '#fff',
    buttonNormalColor: '#f8d7da',
    buttonHoverColor: '#90ee90'
  }
})

const {fgColor, bgColor, buttonNormalColor, buttonHoverColor} = getColors()

</script>

<template>
  <NConfigProvider>
    <!-- 使用NConfigProvider包裹一层，否则编辑器可能报警告[Vue warn]: injection "n-config-provider" not found.
at <DataTableBody ref="bodyInstRef" bodyStyle= {maxHeight: '10000px', minHeight: undefined} showHeader=false  ... > -->
    <div>
      <n-tag>方案1计算结果(ARES方案) {{ gap_rails }}
        <button @click="create3D">生成3D模型</button>
      </n-tag>
      <n-slider v-model:value="table_height" :min="200" :max="2000" :step="10" style="max-width: 100%"/>
      <n-data-table :columns="columns()" :data="tableData" :pagination="false" :bordered="true"
                    :max-height="table_height"
                    :row-class-name="rowClassName" striped default-expand-all/>

    </div>
  </NConfigProvider>

</template>

<style scoped>
.n-tag {
  border: coral;
  background-color: coral;
  width: 100%;
}

:deep(.n-data-table-th) {
  padding: 8px;
}

:deep(.n-data-table-td) {
  padding: 8px;
}

:deep(.sep td) {
  background-color: aqua !important;
}

:deep(div.n-input) {
  height: 30px;
  --n-height: 20px;
  padding: 0;
  margin: 0;
}

button {
  background-color: v-bind(buttonNormalColor);
  color: black;
  border: 0;
  padding: 3px 50px;
  margin: 100px;
  font-size: 16px;
  box-sizing: content-box;
  border-radius: 5px;
}

button:hover {
  background-color: v-bind(buttonHoverColor);
}
</style>
